/*
 * Copyright (c) 2009 John Pritchard, JBXML Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jbxml;

/**
 * @author jdp
 */
public class Relative
    extends lxl.beans.Relative
    implements Value
{

    public Relative(Component owner, String property){
        super(owner,property);
    }
    public Relative(Component owner, String property, String s){
        super(owner,property,s);
    }


    public Component getParent(){
        return ((Component)this.owner).getParent();
    }
    public Object parentValue(){
        Component parent = this.getParent();
        if (null != parent){
            Reflector parentReflect = parent.getReflect();
            return parentReflect.get(this.property);
        }
        else
            return null;
    }
    public float floatValue(){
        if (By.px == this.by)
            return this.base;
        else if (By.pc == this.by){
            Object parentValue = this.parentValue();
            if (parentValue instanceof Number)

                return this.floatValue( (Number)parentValue);

            else if (null != parentValue)
                throw new IllegalArgumentException("Parent value of '"+this.property+"' is not a number ("+parentValue.getClass()+").");
            else
                throw new IllegalArgumentException("Parent value of '"+this.property+"' is null.");
        }
        else
            throw new UnsupportedOperationException();
    }

}
